<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row :gutter="24">
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="fieldName"
              label="参数名称">
              <a-input placeholder="请输入参数名称" v-model="model.fieldName"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="fieldRemark"
              label="参数说明">
              <a-input placeholder="请输入参数说明" v-model="model.fieldRemark"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="valueType"
              label="值类型">
              <j-dict-select-tag placeholder="请输入值类型" v-model="model.valueType" dict-code="value_type"
                                 @change="dictTypeChange" ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="required"
              label="是否必填">
              <a-radio-group  v-model="model.required">
                <a-radio value="1">是</a-radio>
                <a-radio value="0">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="dictType"
              label="字典类型">
              <j-dict-select-tag showSearch :filterOption="filterOption" placeholder="请输入字典类型" v-model="model.dictType" dict-code="sys_dict,dict_name,dict_code"
               @change="dictTypeChange" ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="默认值">
              <a-input v-if="model.valueType == '1'" placeholder="请输入默认值" v-model="model.defaultValue" />
              <j-dict-select-tag v-else placeholder="请输入字典类型" v-model="model.defaultValue" :dict-code="model.dictType"
                @change="defaultValueChange"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="排序">
              <a-input-number placeholder="请输入排序" v-model="model.sort"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'

export default {
  name: 'TbGenerateItemConfigModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      input_width: { width: '270px' },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        fieldName: [
              { required: true, message: '请输入参数名称!' },
              { max: 20, message: '长度不能大于20'}],
        fieldRemark: [
              { required: true, message: '请输入参数说明!' },
              { max: 200, message: '长度不能大于200'}],
        valueType: [
              { required: true, message: '请输入值类型!' }
              ],
        required:[
              { required: true, message: '请输入是否必填' }],
        dictType: [
              { max: 20, message: '长度不能大于20'},
              {validator: this.dictTypeValidate}
            ],
        defaultValue: [
            { max: 20, message: '长度不能大于20', trigger: 'blur' }]
      },
      url: {
        add: '/generator/TbGenerateItemConfig/add',
        edit: '/generator/TbGenerateItemConfig/edit',
      }
      ,
    }
  },
  created() {
  },
  methods: {
    dictTypeValidate(rule, value, callback){
      let valueType = this.model.valueType
      if(valueType && valueType != '1' && !value){
        callback('请选择字典类型');
      }else {
        callback();
      }
    },
    dictTypeChange(value){
        this.model.defaultValue = undefined;
    },
    defaultValueChange(value){
      this.$forceUpdate();
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.model = Object.assign({}, record)
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    filterOption(input, option) {
      return (option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0)
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>

<style lang="less" scoped>

</style>
